package com.github.webflux.handler;

import com.github.webflux.dao.CityRepositoryMongoDB;
import com.github.webflux.domain.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/8/23 21:38
 */
@Component
public class CityHandlerMongoDB
{
    private final CityRepositoryMongoDB cityRepository;

    @Autowired
    public CityHandlerMongoDB(CityRepositoryMongoDB cityRepository)
    {
        this.cityRepository = cityRepository;
    }

    public Mono<City> save(City city)
    {
        return cityRepository.save(city);
    }

    public Mono<City> findCityById(Long id)
    {

        return cityRepository.findById(id);
    }

    public Flux<City> findAllCity()
    {

        return cityRepository.findAll();
    }

    public Mono<City> modifyCity(City city)
    {

        return cityRepository.save(city);
    }

    public Mono<Long> deleteCity(Long id)
    {
        return cityRepository.deleteById(id).flatMap(mono -> Mono.create(cityMonoSink -> cityMonoSink.success(id)));
    }
}
